
#include "renorm_custom_tiling.h"
#include "register/op_def_registry.h"
#include "graph/utils/type_utils.h"
#include "tiling/platform/platform_ascendc.h"


namespace optiling {
constexpr uint32_t BLOCK_SIZE = 32;

static ge::graphStatus TilingFunc(gert::TilingContext* context)
{

//  RenormCustomTilingData tiling;
//  const gert::StorageShape* x1_shape = context->GetInputShape(0);
//  int32_t data_sz = 1;
//  for (int i = 0; i < x1_shape->GetStorageShape().GetDimNum(); i++)
//    data_sz *= x1_shape->GetStorageShape().GetDim(i);
//  tiling.set_size(data_sz);
//  context->SetBlockDim(8);
//  tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
//  context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
//
//  return ge::GRAPH_SUCCESS;

    RenormCustomTilingData tiling;
    auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    auto blockDim = ascendcPlatform.GetCoreNum();

    // 获取元素总数、行数、列数
    const gert::StorageShape* x1_shape = context->GetInputShape(0);
    uint32_t totalLength = 1;
    uint32_t rawNum = 0;
    uint32_t columnNum = 0;
    for (int i = 0; i < x1_shape->GetStorageShape().GetDimNum(); i++){
        totalLength *= x1_shape->GetStorageShape().GetDim(i);
        if(i == 0)
            rawNum = x1_shape->GetStorageShape().GetDim(i);
        else if(i == 1)
            columnNum = x1_shape->GetStorageShape().GetDim(i);
    }

    // 获取算子参数
    const gert::RuntimeAttrs *attrs = context->GetAttrs();
    const int *dim = attrs->GetAttrPointer<int>(0);
    const float *p = attrs->GetAttrPointer<float>(1);
    const float *max_norm = attrs->GetAttrPointer<float>(2);

    // 获取数据类型Byte数
    uint32_t dataTypeSize = 0;
    ge::TypeUtils::GetDataTypeLength(context->GetInputDesc(0)->GetDataType(), dataTypeSize);

    // 部署计算tilling
    uint32_t alignFlag;
    uint32_t tileLength;
    uint32_t tileNum;
    uint32_t blockLength;
    uint32_t tailNum;
    uint32_t EvenCore;

    if (*dim == 0)
    {
        // 核心数大于操作数
        if (rawNum < blockDim)
        {
            blockDim = rawNum;
        }
        // tilling不对齐
        if (((columnNum * dataTypeSize) % BLOCK_SIZE) == 0)
        {
            alignFlag = 1;
            tileLength = columnNum;
        }
        else{
            alignFlag = 0;
            uint32_t alignNum = (BLOCK_SIZE - (columnNum * dataTypeSize) % BLOCK_SIZE) / dataTypeSize;
            tileLength = columnNum + alignNum;//111
        }

        tileNum = rawNum / blockDim;
        blockLength = tileNum * columnNum;
        tailNum = tileNum + (rawNum % blockDim);

    }
    else if (*dim == 1)
    {
        // 核心数大于操作数
        if (columnNum < blockDim)
        {
            blockDim = columnNum;
        }
        tileNum = columnNum / blockDim;
        tileLength = rawNum;
        blockLength = tileNum * tileLength;
        tailNum = tileNum + (columnNum % blockDim);
    }
    EvenCore = blockDim - 1; // 最后一个核的id

    // 为tilling data赋值
    tiling.set_totalLength(totalLength);
    tiling.set_rawNum(rawNum);
    tiling.set_columnNum(columnNum);
    tiling.set_dataTypeSize(dataTypeSize);
    tiling.set_alignFlag(alignFlag);
    tiling.set_tileLength(tileLength);
    tiling.set_tileNum(tileNum);
    tiling.set_blockLength(blockLength);
    tiling.set_tailNum(tailNum);
    tiling.set_EvenCore(EvenCore);
    tiling.set_dim(*dim);
    tiling.set_p(*p);
    tiling.set_max_norm(*max_norm);

    context->SetBlockDim(blockDim);
    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    // size_t *currentWorkspace = context->GetWorkspaceSizes(1);
    // currentWorkspace[0] = 0;
    size_t systemWorkspaceSize = ascendcPlatform.GetLibApiWorkSpaceSize();
    size_t *currentWorkspace = context->GetWorkspaceSizes(1);
    currentWorkspace[0]= systemWorkspaceSize;
    return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
static ge::graphStatus InferDataType(gert::InferDataTypeContext *context)
{
const auto inputDataType = context->GetInputDataType(0);
context->SetOutputDataType(0, inputDataType);
return ge::GRAPH_SUCCESS;
}
}


namespace ops {
class RenormCustom : public OpDef {
public:
    explicit RenormCustom(const char* name) : OpDef(name)
    {
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_BF16, ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("z")
            .ParamType(REQUIRED)
            .DataType({ge::DT_BF16, ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Attr("dim").AttrType(OPTIONAL).Int(0);
        this->Attr("p").AttrType(OPTIONAL).Float(1.0);
        this->Attr("max_norm").AttrType(OPTIONAL).Float(5.0);

        this->SetInferShape(ge::InferShape).SetInferDataType(ge::InferDataType);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend910b");

    }
};

OP_ADD(RenormCustom);
}
